<?php


class QuestionTest extends CActiveRecord
{
	
	public function tableName()
	{
		return 'question_test';
	}

	
	public function rules()
	{
		
		
		return array(
			array('create_date', 'required'),
			array('order_number, status, scores', 'numerical', 'integerOnly'=>true),
			array('display_name, code_name, content', 'length', 'max'=>200),
			array('content', 'length', 'max'=>1000),
			array('create_user_id', 'length', 'max'=>11),
			
			
			array('id, order_number, display_name, code_name, content, status, scores, create_date, modify_date, create_user_id', 'safe', 'on'=>'search'),
		);
	}

	
	public function relations()
	{
		
		
		return array(
			'questionAnswers' => array(self::HAS_MANY, 'QuestionAnswer', 'question_test_id'),
			'createUser' => array(self::BELONGS_TO, 'AppUser', 'create_user_id'),
		);
	}

	
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'order_number' => 'Order Number',
			'code_name' => 'Code Name',
			'display_name' => 'Display Name',
			'content' => 'Content',
			'status' => 'Status',
			'scores' => 'Scores',
			'create_date' => 'Create Date',
			'modify_date' => 'Modify Date',
			'create_user_id' => 'Create User',
		);
	}

	
	public function search()
	{
		

		$V0v5vsnfxezw=new CDbCriteria;

		$V0v5vsnfxezw->compare('id',$this->id,true);
		$V0v5vsnfxezw->compare('order_number',$this->order_number);
		$V0v5vsnfxezw->compare('code_name',$this->code_name,true);
		$V0v5vsnfxezw->compare('display_name',$this->display_name,true);
		$V0v5vsnfxezw->compare('content',$this->content,true);
		$V0v5vsnfxezw->compare('status',$this->status);
		$V0v5vsnfxezw->compare('scores',$this->scores);
		$V0v5vsnfxezw->compare('create_date',$this->create_date,true);
		$V0v5vsnfxezw->compare('modify_date',$this->modify_date,true);
		$V0v5vsnfxezw->compare('create_user_id',$this->create_user_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$V0v5vsnfxezw,
		));
	}

	
	public static function model($Vgmajjibziuy=__CLASS__)
	{
		return parent::model($Vgmajjibziuy);
	}
	
	public static function findQuestions($Vtkshvgl1ivp, $Vfyl0i5chevf, $Vtebq5vsa45o, $Vmq5toeun05t, $V5as2qkxgfs4 = -1, $Vrlyvhau3qpi = 1,$Vtkshvgl1ivpNumber=-1) {
    	$V24noxecbnll = "1";
        if($V5as2qkxgfs4 != -1) {
            $V24noxecbnll = "id = ".$V5as2qkxgfs4;
        }
        else {
        	if($Vrlyvhau3qpi > -1) {
	            $V24noxecbnll .= " AND (status = ".$Vrlyvhau3qpi.")"; 
        	}
            if (!empty ($Vmq5toeun05t)) {
                $V24noxecbnll .= " AND (t.display_name LIKE '%$Vmq5toeun05t%') ";
            }
        }
        if($Vtkshvgl1ivpNumber > 0){
                $V24noxecbnll .= " AND (t.order_number = ".$Vtkshvgl1ivpNumber.")";
        }
        $Vxun03s52uac = Yii::app()->db->createCommand()
                ->selectDistinct('t.id,t.order_number,t.create_date,t.display_name,t.status, t.scores, t.content')
                ->from('question_test as t')
                ->where($V24noxecbnll) 
                ->limit($Vtebq5vsa45o, $Vfyl0i5chevf * $Vtebq5vsa45o)
                ->order($Vtkshvgl1ivp)
                ->queryAll();
        $Vfx3dng1y1f3 = Yii::app()->db->createCommand()
                ->select('count(id)')
                ->from('question_test as t')
                ->where($V24noxecbnll) 
                ->queryScalar();
        
        
        
        
        
        return array(
            'total_result'=>$Vfx3dng1y1f3,
            'keyword'=>$Vmq5toeun05t,
            'page_number'=>$Vfyl0i5chevf,
            'page_size'=>$Vtebq5vsa45o,
            'total_page'=>($Vfx3dng1y1f3 - ($Vfx3dng1y1f3 % $Vtebq5vsa45o))/$Vtebq5vsa45o + (($Vfx3dng1y1f3 % $Vtebq5vsa45o ===0)?0:1),
            'data'=>$Vxun03s52uac
        );
    }
}
